﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Windows.Forms;
using System.Xml;

namespace UpdateApp
{
    static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main(string[] args)
        {
            Application.ThreadException += (sender, e) =>
            {
                MessageBox.Show(e.Exception.Message);
            };
            var temp = Update.HasNewVersion();
            if (temp.Result)
            {
                if (Update.IsSystemPath() && !Update.IsAdministrator())
                {
                    Update.ReStartAppWithAdministrator();
                }
                else
                {
                    var appPath = Directory.GetParent(Application.StartupPath).FullName;
                    var localxdoc = new XmlDocument();
                    localxdoc.Load(appPath + "\\LocalVersion.xml");
                    //更新版本号到本地
                    XmlNode ele = localxdoc.SelectSingleNode("//version");
                    ele.InnerText = temp.ServerVersion;
                    localxdoc.Save(appPath + "\\LocalVersion.xml");
                    Application.EnableVisualStyles();
                    Application.SetCompatibleTextRenderingDefault(false);
                    Application.Run(new Form1(temp));
                }

            }
            else
            {
                string language = GetConfigValue("Language");
                if (language == "Chinese")
                {
                    if (MessageBox.Show("噢,攻城狮没发布新版本", "提示", MessageBoxButtons.OK,
                        MessageBoxIcon.Asterisk) == DialogResult.OK)
                    {
                        Environment.Exit(0);

                    }

                }
                else
                {
                    if (MessageBox.Show("Oh, No new version", "Prompt", MessageBoxButtons.OK,
                        MessageBoxIcon.Asterisk) == DialogResult.OK)
                    {
                        Environment.Exit(0);
                    }
                }
                // MessageBox.Show("暂时无新版本", "提示", MessageBoxButtons.OK, MessageBoxIcon.Question);
                // Environment.Exit(0);
            }
        }
        static string GetConfigValue(string appKey)
        {
            XmlDocument xDoc = new XmlDocument();
            try
            {
                string path = new DirectoryInfo("../").FullName + "YG1.exe.config";//当前应用程序路径的上级目录
                xDoc.Load(path);
                XmlNode xNode;
                XmlElement xElem;
                xNode = xDoc.SelectSingleNode("//appSettings");
                xElem = (XmlElement)xNode.SelectSingleNode("//add[@key='" + appKey + "']");
                if (xElem != null)
                    return xElem.GetAttribute("value");
                else
                    return "";
            }
            catch (Exception)
            {
                return "";
            }
        }
    }
}
